<?php

session_start();

class GL_Login extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function register()
    {
        echo $this->user->save();
    }

    public function index()
    {
        $result = $this->user->login();

        if ($result) {
            foreach ($result as $row) {
                $sess = array(
                    'id' => $row->id,
                    'email' => $row->email,
                    'name' => $row->name,
                    'password' => $row->password,
                    'permission' => $row->permission
                );
                $this->session->set_userdata('logged_in', $sess);
            }
            echo 1;
        } else echo 0;
    }

    public function logout()
    {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('../');
    }

    public function forgot(){
        $email = $this->input->post('email');
        $pass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6);

        if ($this->user->change_password($email,$pass)){

            $this->load->library('email');

            $config['protocol']    = 'smtp';
            $config['smtp_host']    = 'ssl://smtp.gmail.com';
            $config['smtp_port']    = '465';
            $config['smtp_timeout'] = '7';
            $config['smtp_user']    = '11520137@gm.uit.edu.vn';
            $config['smtp_pass']    = '0915827371';
            $config['charset']    = 'utf-8';
            $config['newline']    = "\r\n";
            $config['mailtype'] = 'html';
            $config['validation'] = TRUE;

            $this->email->initialize($config);


            $this->email->from('support@graphicland.com', 'GraphicLand');
            $this->email->to($email);
            $this->email->subject('Your new password on Graphicland');


            $this->email->message('Your new password on Graphicland: '.$pass);
            $this->email->send();

            echo 1;

        } else echo 0;
    }
}